<?php 
	namespace Home\Controller;
	use Home\Controller\CommonController;
	use Home\Model\Students;
	class StuController extends CommonController{
		public $stu;
		public function __init(){
			$this->stu=new Students;
		}
		public function index(){
			$count=$this->stu->join('users','students.`to`','=','users.uid')->where('`from`',$_SESSION['userinfo']['uid'])->orderBy('addtime','DESC')->count();
			$page=Page::desc(['pre'=>'上一页', 'next'=>'下一页'])->row(10)->make($count);
			$data=$this->stu->join('users','students.`to`','=','users.uid')->where('`from`',$_SESSION['userinfo']['uid'])->orderBy('addtime','DESC')->limit(Page::limit())->get();
			View::with('page',$page);
			View::with('data',$data);
			View::make();
		}

		public function addStu(){
			if(IS_POST){
				$_POST['addtime']=time();
				$_POST['from']=$_SESSION['userinfo']['uid'];
				$_POST['to']=$this->svgStu();
				if($this->stu->add($_POST)){
					View::success('添加成功',U('index'));
				}else{
					View::error('添加失败');
				}
			}
			View::make();
		}

		private function svgStu(){
			$uids=$user=Db::table('users')->where('class',1)->where('statu',0)->lists('uid');
			$arr=array();
			foreach($uids as $v){
				$arr[$v]=Db::table('students')->where('`to`',$v)->count();
			}
			krsort($arr);
			$arr=array_flip($arr);
			return current($arr);
		}
		public function allStu(){
			$count=Db::table('students s')->join('users u','s.to','=','u.uid')->count();
			$page=Page::desc(['pre'=>'上一页', 'next'=>'下一页'])->row(10)->make($count);
			$data=Db::table('students s')->join('users u','s.to','=','u.uid')->orderBy('addtime','desc')->limit(Page::limit())->get();
			View::with('page',$page);
			View::with('data',$data);
			View::make();
		}
	}
?>